package com.mrobuy.jd.category;

import com.gavel.database.SQLExecutor;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.jdvc.entity.JDCategory;
import com.mrobuy.jdvc.entity.VCCategory;

import java.util.List;

/**
 *  VC 类目加载
 *
 */
public class VCCategotyLoader {

    public static void main(String[] args) throws Exception {

        load(JdClientFactory.jdClient(), "wysgyp");

    }

    public static void load(JdClient client, String vccode) throws Exception {


        List<VCCategory> vcCategories = Jos.categoryFindClient(client).call();

        for (VCCategory vcCategory : vcCategories) {
            try {
                VCCategory exist =  SQLExecutor.executeQueryBean("select * from VC_CATEGORY where ID = ? and VCCODE = ? ", VCCategory.class, vcCategory.getId(), vccode);
                if ( exist!=null ) {
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            JDCategory parent = SQLExecutor.executeQueryBean("select * from JD_CATEGORY where CID = ? ", JDCategory.class, vcCategory.getId());
            if ( parent!=null ) {
                vcCategory.setPid(parent.getPid());
                parent = SQLExecutor.executeQueryBean("select * from JD_CATEGORY where CID = ? ", JDCategory.class, parent.getPid());
                if ( parent!=null ) {
                    vcCategory.setPname(parent.getName());
                }
            }

            vcCategory.setVccode(vccode);
            SQLExecutor.insert(vcCategory);
        }


    }
}
